package com.jt.provider.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.jt.com.jt.provider.service.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.atomic.AtomicLong;

@RestController
@RequestMapping("/provider")
public class ProviderSentinelController {
    @GetMapping("/sentinel01")
    public String doSentinel01(){
        return "sentinel 01 test";
    }
    @GetMapping("/sentinel02")
    public String doSentinel02(){
        return "sentinel 02 test";
    }
    @Autowired
    private ResourcesService resourcesService;

    @GetMapping("/sentinel03")
    public String doSentinel03(){
        return resourcesService.doGetResource();
        //return "sentinel 03 test";
    }
    //构建一个AtomicLong对象，提供了线程安全的自增，自减的操作
    private AtomicLong atomicLong = new AtomicLong(1);
    private int count;
    @GetMapping("/sentinel04")
    public String doSentinel04() throws InterruptedException {
        /*count++;
        if(count%2==0){    会出现线程安全问题
            Thread.sleep(200);
        }*/
        long num = atomicLong.getAndIncrement();
        if (num%2==0){
            Thread.sleep(200);
        }
        return "sentinel 04 test";
    }
    @GetMapping("/sentinel/doFindById")
    @SentinelResource("resourse")
    public String doFindById(@RequestParam("id") String id){
        return "resource id is "+id;
    }


}
